package org.RedCode.btn1;

import org.RedCode.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

public class Btn1View extends View {
	
	Bitmap m_Background;
	Bitmap m_quad01;
	Bitmap m_quad02;
	Bitmap m_circle;
	Bitmap m_triangle;
	int a = 0;
	int b = 1;
	int c = 2;
	int d = 3;
	int e = 4;
	int f = 5;
	int g = 6;
	int i = 7;
	int j = 8;
	int z = 9;
	int m_Shuffle[][];
	int w,h;
	int o = (int)(Math.random()*8);
	int p = (int)(Math.random()*8);
	
	public Btn1View(Context context) {
		super(context);
		
		m_Background = BitmapFactory.decodeResource(getResources(), R.drawable.background,null);
		m_quad01 = BitmapFactory.decodeResource(getResources(), R.drawable.quadrangle01,null);
		m_quad02 = BitmapFactory.decodeResource(getResources(), R.drawable.quadrangle02,null);
		m_circle = BitmapFactory.decodeResource(getResources(), R.drawable.circle,null);
		m_triangle = BitmapFactory.decodeResource(getResources(), R.drawable.triangle,null);
		m_Shuffle = new int[9][9];
		w=m_quad01.getWidth();
		h=m_quad01.getHeight();
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawBitmap(m_Background, 0, 0, null);
		
		for(int y=0; y<9; y++) {
			for(int x=0; x<9; x++) {
				canvas.drawBitmap(m_quad01, 35 + x*w, 35 + y*h, null);
				if(m_Shuffle[x][y] == z)
					canvas.drawBitmap(m_quad02, 35 + x*w, 35 + y*h, null);
			}
		}
		canvas.drawBitmap(m_circle, 35 + o*w, 35 + p*h, null);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int px = (int)event.getX();
		int py = (int)event.getY();
		setShuffle02();
		
		if(event.getAction() == MotionEvent.ACTION_MOVE) {	
			Rect box = new Rect(35+o*w, 35+p*h, 35+o*w + w, 35+p*h + h);
			if(box.contains(px,py)) {
				if(m_Shuffle[o][p] == a) {
					m_Shuffle[o][p] = z;
					m_Shuffle[o+1][p] = z;
					m_Shuffle[o-1][p] = z;
					m_Shuffle[o][p+1] = z;
				}
				else if(m_Shuffle[o][p] == b) {
					m_Shuffle[o][p] = z;
					m_Shuffle[o+1][p] = z;
					m_Shuffle[o][p+1] = z;
					m_Shuffle[o][p-1] = z;
				}
				else if(m_Shuffle[o][p] == c) {
					m_Shuffle[o][p] = z;
					m_Shuffle[o-1][p] = z;
					m_Shuffle[o][p+1] = z;
					m_Shuffle[o][p-1] = z;
				}
				else if(m_Shuffle[o][p] == d) {
					m_Shuffle[o][p] = z;
					m_Shuffle[o+1][p] = z;
					m_Shuffle[o-1][p] = z;
					m_Shuffle[o][p-1] = z;
				}
				else if(m_Shuffle[o][p] == e) {
					m_Shuffle[o][p] = z;
					m_Shuffle[o+1][p] = z;
					m_Shuffle[o][p+1] = z;
					m_Shuffle[o-1][p] = z;
					m_Shuffle[o][p-1] = z;
				}
				else if(m_Shuffle[o][p] == f) {
					m_Shuffle[o][p] = z;
					m_Shuffle[o+1][p] = z;
					m_Shuffle[o][p+1] = z;
				}
				else if(m_Shuffle[o][p] == g) {
					m_Shuffle[o][p] = z;
					m_Shuffle[o-1][p] = z;
					m_Shuffle[o][p+1] = z;
				}
				else if(m_Shuffle[o][p] == i) {
					m_Shuffle[o][p] = z;
					m_Shuffle[o+1][p] = z;
					m_Shuffle[o][p-1] = z;
				}
				else if(m_Shuffle[o][p] == j) {
					m_Shuffle[o][p] = z;
					m_Shuffle[o-1][p] = z;
					m_Shuffle[o][p-1] = z;
				}
			}
		}
		invalidate();
		return true;
	}
	
	public void setShuffle02() {
		for(int y=0; y<1; y++) {
			for(int x=1; x<8; x++)
				m_Shuffle[x][y] = a;
		}
		for(int y=1; y<8; y++) {
			for(int x=0; x<1; x++)
				m_Shuffle[x][y] = b;
		}
		for(int y=1; y<8; y++) {
			for(int x=8; x<9; x++)
				m_Shuffle[x][y] = c;
		}
		for(int y=8; y<9; y++) {
			for(int x=1; x<8; x++)
				m_Shuffle[x][y] = d;
		}
		for(int y=1; y<8; y++) {
			for(int x=1; x<8; x++)
				m_Shuffle[x][y] = e;
		}
		for(int y=0; y<1; y++) {
			for(int x=0; x<1; x++)
				m_Shuffle[x][y] = f;
		}
		for(int y=0; y<1; y++) {
			for(int x=8; x<9; x++)
				m_Shuffle[x][y] = g;
		}
		for(int y=8; y<9; y++) {
			for(int x=0; x<1; x++)
				m_Shuffle[x][y] = i;
		}
		for(int y=8; y<9; y++) {
			for(int x=8; x<9; x++)
				m_Shuffle[x][y] = j;
		}
	}
}
